const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    showCellItem: null,
    types_array: ["maintenance", "ClaimReport", "ClaimReport"],   //ClaimReport 保险请求带 maintenance 维保请求带
    orderDialog:{
      quit: "quit",
      AndSoOn: "AndSoOn",
      continue:"Continue"
    },
    orderDialog2: {
      AndSoOn: "AndSoOn2",
      continue: "Continue2",
      quit: "quit2"
    },
    showDialog: false,
    showDialog2: false, 
    queryHis: "",
    show_order: true   
  },

  onLoad(options) {
    console.log(options);
    let td = this.data;
    this.setData({
      orderDialog: td.orderDialog,
    });
    if (options.pay_info){
      let pay_info = JSON.parse(options.pay_info);
      this.setData({
        vin_code: pay_info.Vin || pay_info.vin,
        brand: pay_info.PP || pay_info.pp + " " + pay_info.CX,
        types: options.types,
        queryImg: options.queryImg || ''
      });

      if (options.queryImg){
        this.getVinGetVehicleModel(pay_info.Vin || pay_info.vin);        
      }else{
        if (options.types == 0) {
          this.advanceOrder({
            queryHis: "",
            CX: pay_info.CX,
            PP: pay_info.PP || pay_info.pp,
            vin: pay_info.Vin || pay_info.vin
          });
        } else if (options.types == 1) {
          this.advanceClaimOrder({
            CX: pay_info.CX,
            PP: pay_info.PP || pay_info.pp,
            vin: pay_info.Vin || pay_info.vin
          });
        }
      }
      
      
   }
  },

  /*分享*/ 
  onShareAppMessage(){

  },
  
  getCell(e){
    this.setData({
      showCellItem: e.currentTarget.dataset.index
    });
  },

  /*vin 码检测*/ 
  ggmInput(e){
    let _this = this, td = this.data, vin = e.detail.value;
    let val_2 = "";
    for (let i of vin) {
      if (!/[\u4e00-\u9fa5]/.test(i)) {
        val_2 += i;
      }
    }
    
    if (vin.length == 17) {
       setTimeout(()=>{
         _this.getVinGetVehicleModel(val_2.toUpperCase());
       },3);
    } else {
      _this.setData({
        vin_code: val_2.toUpperCase(),
        brand: ""
      });
    }
  },

  getVinGetVehicleModel(vin){
    console.log(vin);
    let _this = this, td = this.data
    wx.showLoading({
      title: "VIN码检验中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.getVinGetVehicleModel, {
      vin: vin
    }, (status, res) => {
      wx.hideLoading();
      if (res.data.status === "success") {
        let pay_info = res.data.result;
        _this.setData({
          vin_code: pay_info.Vin || pay_info.vin,
          brand: pay_info.PP || pay_info.pp + " " + pay_info.CX,
        });

        if (td.types == 0) {
          _this.advanceOrder({
            queryHis: "",
            CX: pay_info.CX,
            PP: pay_info.PP || pay_info.pp,
            vin: pay_info.Vin || pay_info.vin
          });
        } else if (td.types == 1) {
          this.advanceClaimOrder({
            CX: pay_info.CX,
            PP: pay_info.PP || pay_info.pp,
            vin: pay_info.Vin || pay_info.vin
          });
        }
      } else {
        _this.setData({
          showHintInfo: true,
          show_order: false
        });
      }
    });
  },

  // 预订单
  advanceOrder(params) {
    let _this = this, td = this.data;
    params.thirdCookie = wx.getStorageSync("thirdCookie");
    SERVICE.POST(SERVICE.URL.advanceOrder, params, (status, res) => {
      let ResData = res.data;
      console.log(ResData);
      if (ResData.isquery.res_status == "success"){
        td.orderDialog2.text = ResData.isquery.messge;
        _this.setData({
          showDialog2: true,
          orderDialog2: td.orderDialog2
        });
      } if (ResData.status == "success") {
        _this.setData({
          order_data: ResData,
          show_order: true
        });
      } else if (ResData.status == "absmiantence") {
        _this.setData({
          order_data: ResData,
          show_order: false
        });
      } else if (ResData.status == "relmaintence") {
        td.orderDialog.text = ResData.message;
        _this.setData({
          order_data: ResData,
          showDialog: true,
          orderDialog: td.orderDialog
        });
      }
     
    });
  },
  advanceClaimOrder(params){
    let _this = this, td = this.data;
    params.thirdCookie = wx.getStorageSync("thirdCookie");
    SERVICE.POST(SERVICE.URL.advanceClaimOrder, params, (status, res) => {
      let ResData = res.data;
      console.log(ResData);
      if (ResData.status == "success"){
        if (ResData.isquery.res_status == "null"){
          _this.setData({
            order_data: ResData,
            show_order: true
          });
        }else{
          td.orderDialog2.text = ResData.isquery.messge;
          _this.setData({
            order_data: ResData,            
            showDialog2: true,
            orderDialog2: td.orderDialog2,
            show_order: true            
          });
        }
        
      }
    });
  },
  
  formSubmit(e){
    let form_obj = e.detail.value;
    if (!form_obj.radio_group){
      wx.showToast({
        title: "请选择支付方式",
        icon: "none",
        mask: true
      });
    } else if (form_obj.agreement.length == 0){
      wx.showToast({
        title: "未同意车大师用户协议",
        icon: "none",
        mask: true
      });
    }else{
      let td = this.data;
      if (td.types == 0){
          this.createMainOrder({
            price: td.order_data.price,
            CX: td.order_data.CX,
            vin: td.order_data.vin,
            pp: td.order_data.PP
          },form_obj);
      } else if (td.types == 1){
         this.createClaimOrder({
           CX: td.order_data.CX,
           vin: td.order_data.vin,
           pp: td.order_data.PP
         }, form_obj);
      }
     
    }
  },

  /*车况 创建订单*/
  createMainOrder(params,formObj) {
    params.thirdCookie = wx.getStorageSync("thirdCookie");    
    let _this = this, td = this.data;
    wx.showLoading({
      title: "创建订单中...",
      mask: true
    });
    
    SERVICE.POST(SERVICE.URL.createMainOrder,params,(stauts,res) => {
      wx.hideLoading();
      if(res.data.status == "success"){
        let order_data = res.data;
        if (formObj.radio_group == 0) {
          console.log(0);
        _this.balancePay({
          queryHis: td.queryHis,
          thirdCookie: wx.getStorageSync("thirdCookie"),
          price: order_data.price,
          orderId: order_data.orderId,
          business: td.types_array[td.types]
        });
        } else if (formObj.radio_group == 1){
          console.log(1);
        _this.masterMionPay({
          queryHis: td.queryHis,
          thirdCookie: wx.getStorageSync("thirdCookie"),
          price: order_data.price,
          orderId: order_data.orderId,
          business: td.types_array[td.types]
        });
        } else if (formObj.radio_group == 2) {
          console.log(2);
        wx.login({
          success: function (loginRes) {
            wx.setStorageSync("loginInfo", loginRes);
            _this.wxpayverify({
              queryHis: td.queryHis,
              thirdCookie: wx.getStorageSync("thirdCookie"),
              price: order_data.price,
              code: wx.getStorageSync("loginInfo").code,
              orderId: order_data.orderId,
              business: td.types_array[td.types]
            });
          }
        })
      } 
      }else{
        wx.showToast({
          title: "创建订单失败",
          icon: "none",
          mask: true
        });
      }
      

      

    });
  },

  /*创建订单*/
  createClaimOrder(params, formObj) {
    let _this = this, td = this.data;
      params.thirdCookie = wx.getStorageSync("thirdCookie");
    wx.showLoading({
      title: "创建订单中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.createClaimOrder, params, (stauts, res) => {
      wx.hideLoading();
      if (res.data.status == "success") {
        let order_data = res.data;
        if (formObj.radio_group == 0) {
          _this.balancePay({
            queryHis: td.queryHis,
            thirdCookie: wx.getStorageSync("thirdCookie"),
            price: order_data.price,
            orderId: order_data.orderId,
            business: td.types_array[td.types]
          });
        } else if (formObj.radio_group == 1) {
          _this.masterMionPay({
            queryHis: td.queryHis,
            thirdCookie: wx.getStorageSync("thirdCookie"),
            price: order_data.price,
            orderId: order_data.orderId,
            business: td.types_array[td.types]
          });
        } else if (formObj.radio_group == 2) {
          wx.login({
            success: function (loginRes) {
              wx.setStorageSync("loginInfo", loginRes);
              _this.wxpayverify({
                queryHis: td.queryHis,
                thirdCookie: wx.getStorageSync("thirdCookie"),
                price: order_data.price,
                code: wx.getStorageSync("loginInfo").code,
                orderId: order_data.orderId,
                business: td.types_array[td.types]
              });
            }
          })
        }
      } else {
        wx.showToast({
          title: "创建订单失败",
          icon: "none",
          mask: true
        });
      }

    });
  },
  
  /*微信支付*/
  wxpayverify(params) {
    wx.showLoading({
      title: "支付中...",
      mask: true
    });
    let _this = this, td = this.data;
    
    SERVICE.POST(SERVICE.URL.wxpayverify, params, (stauts, res) => {
      wx.hideLoading();      
      wx.requestPayment({
        'timeStamp': res.data.timeStamp,
        'nonceStr': res.data.nonceStr,
        'package': 'prepay_id=' + res.data.prepay_id,
        'signType': 'MD5',
        'paySign': res.data.paySign,
        'success': function (res) {
          wx.showModal({
            title: "提示",
            content: "支付成功,是否前往订单记录",
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: "../my-record/my-record?showNavItem=" + td.types,
                  success: function(){
                    _this.setData({
                      queryHis: ""
                    });
                  }
                });
              }
            }
          })
        },
        'fail': function (res) {
          console.log("fail:", res);
          wx.showToast({
            title: "您取消了支付",
            icon: "none",
            mask: true,
            duration: 1500,
            success: function(){
              _this.setData({
                queryHis: ""
              });
            }
          });
        }
      })
    });
  },

  /*余额支付*/
  balancePay(params){
    wx.showLoading({
      title: "支付中...",
      mask: true
    });
    let _this = this, td = this.data;
    
    SERVICE.POST(SERVICE.URL.balancePay, params, (stauts, res) => {
      wx.hideLoading();
      if (res.data.status === "success") {
        wx.showModal({
          title: "提示",
          content: "支付成功,是否前往订单记录",
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: "../my-record/my-record?showNavItem=" + td.types,
                success: function(){
                  _this.setData({
                    queryHis: ""
                  });
                }
              });
            }else{
              _this.setData({
                queryHis: ""
              });
            }
          }
        })
      } else {
        wx.showToast({
          title: "支付失败",
          icon: "none",
          mask: true
        });
      }
    });
  },
  
  /*大师币支付*/
  masterMionPay(params){
    wx.showLoading({
      title: "支付中...",
      mask: true
    });
    let _this = this, td = this.data;
    SERVICE.POST(SERVICE.URL.masterMionPay, params, (stauts, res) => {
      wx.hideLoading();
      if (res.data.status === "success"){
        wx.showModal({
          title: "提示",
          content: "支付成功,是否前往订单记录",
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: "../my-record/my-record?showNavItem=" + td.types,
                success:function(){
                  _this.setData({
                    queryHis: ""
                  });
                }
              });
            }
          }
        })
      }else{
        wx.showToast({
          title: "支付失败",
          icon: "none",
          mask: true,
          success: function(){
            _this.setData({
              queryHis: ""
            });
          }
        });
      }
    });
  },


  AndSoOn(){
    let td = this.data;
    this.setData({
      showDialog: false,
      show_order: false 
    });
  },
  Continue(){
    let td = this.data;
    this.setData({
      showDialog: false,
      show_order: true,
      queryHis: "queryHis"
    });
  },

  quit(){
    this.setData({
      showDialog: false
    });
  },

  AndSoOn2() {
    let td = this.data;    
    this.setData({
      showDialog: false,
      showDialog2: false
    });
    wx.navigateTo({
      url: "../my-record/my-record?showNavItem=" + td.types,
    });
  },
  Continue2() {
    this.setData({
      showDialog2: false
    });
  },
  quit2(){
    this.setData({
      showDialog2: false
    });
  }
});